#/usr/bin/env python
#
# HTML module holds  wrapper classes representing the python instances of their html equivalents. Makes templating easy.
#
import types

class CheckBox(object):
    "python instance of html checkbox, str overloaded for easy template use"
    def __init__(self, checked = False, name="defaultName"):
        "Constructs checkbox. Defaulted to unchecked, with name 'approved'."
        self.string = "<input type=checkbox name=\"" + name + "\""
        self.string += " checked=\"yes\">" if checked else ">"
    def __str__(self):
        "returns html tags for building checkbox"
        return self.string

class RadioButton(object):
    "html Radio button wrapper class, str overloaded for easy template use"
    def __init__(self, name = "defaultRadioName", value="defaultRadioValue", string="defaultRadio", checked=False):
        self.value = value
        if checked:
            self.string = '''<input type="radio" name="%s" value="%s" checked>%s''' % (name, value, string)
        else:
            self.string = '''<input type="radio" name="%s" value="%s">%s''' % (name, value, string)
    def __str__(self):
        return self.string
        
class DropDown(object):
    "python instance of html dropdown, str overloaded for easy template use"
    def __init__(self, options=(), selected="", name = "defaultName", submitOnChange = False, defaultOptionPresent = True):
        "Constructs dropdown. Defaulted to empty list, no options. Defaulted to select top element"
            
        self.options = options
        defaultOption = "" if not defaultOptionPresent else '''<option value="--make selection--">- make selection -</option>'''
        onchange = ""
        if submitOnChange:
            onchange = ''' onChange="this.form.submit()" '''
        self.string = '''<select name="%s"%s>%s''' % (name, onchange, defaultOption)
        for option in options:
            if option == selected :
                self.string += '''<option selected="selected" value="''' + option + '''">''' + option + '''</option>'''
            else:
                self.string += '''<option value="''' + option + '''">''' + option + '''</option>'''
        self.string += '''</select>'''
    def __str__(self):
        "returns html tags for building dropdown"

        return self.string

class MultiSelect(object):
    "python instance of html MultiSelect, str verloaded for easy template use"
    def __init__(self, options=(), selected=(), name = "defaultName", size = 10):
        "Constructs multiselect.  Defaulted to empty list, no options.  Defaulted to select no elements"
        self.string = '''<select multiple size=%d name="%s">''' % (size, name)
        self.options = options
        for option in options:
            if option in selected:
                self.string += '''<option selected="selected" value="%s">%s</option>''' % (option, option)
            else:
                self.string += '''<option vaule="%s">%s</option>''' % (option, option)
        self.string += '''</select>'''
    def __str__(self):
        "returns html tags for building MultiSelect"
        return self.string

class Link(object):
    '''Class to simulate an href HTML tag with title and url'''
    def __init__(self, url, title, target=""):
        self.url = url
        self.title = title
        self.target = '_self' if target == "" else target
    def __str__(self):
        return '''<a href="%s" target="%s">%s</a>''' % (self.url, self.target, self.title)
